<?php
/**
 * Created by PhpStorm.
 * User: tyrans@qq.com
 * Date: 2018/8/16
 * Time: 14:54
 */

use Illuminate\Support\Facades\Log;

if (!function_exists('robotResponse')) {
    function robotResponse($ask)
    {
        $opt = [
            'reqType' => 0,
            "perception" => [
                "inputText" => [
                    "text" => $ask
                ]
            ],
            "userInfo"=>[
                "apiKey"=>'35d770ae50204430b1dd4f9500e59d2d',
                "userId"=>310457
            ]
        ];
        $opt = json_encode($opt);
        $url = 'http://openapi.tuling123.com/openapi/api/v2';
        $data = getCurl($url, $opt);
        return $data;
    }
};

function getCurl($url, $data)
{
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_TIMEOUT, 3);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json'));
    $data = curl_exec($ch);
    return $data;
}

function needJson(\Illuminate\Http\Request $request)
{
    $request = $request ?: \Illuminate\Support\Facades\Request::getFacadeRoot();
    return $request->header('content-type') === 'application/json' || $request->ajax() || $request->has('json');
}

function chatConfig()
{
    $return = [];
    $configs = \App\Models\Wechat\ConfigModel::query()->get();
    foreach ($configs as $config) {
        $return[$config->key] = $config->value;
    }
    return [
        'default' => $return
    ];
}